//
//  MYWelcomeViewController.swift
//  MYWeiBo
//
//  Created by apple on 16/5/14.
//  Copyright © 2016年 YanMo. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class MYWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //准备ui
        prepareUI()
        //加载用户信息
        setIcon()
        MYUserAccountModel.sharedInstance.loadUserInfo { () -> () in
            //使用sdwebimage来加载图片
            self.setIcon()
        }
    }
    //设置用户头像
    private func setIcon(){
        if let avatar_large = MYUserAccountModel.sharedInstance.userAccount?.avatar_large{
            let url = NSURL(string:avatar_large)
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    /**
     *将看到界面的时候，开始动画、
     */
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //开始动画
        moveAnimation()
    }
    
    //动画实现代码
    func moveAnimation(){
   
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            // 要做动画
            // snp_updateConstraints: 更新约束
            self.iconView.snp_updateConstraints(closure: { (make) -> Void in
                // 更新底部约束
                make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height - 160))
            })
            
            // 根据约束重新设置frame值
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
//                   MYPrint(items: "动画完成")
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                        self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //切换到主控制器
                        let appdelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
                        appdelegate.switchRootViewController(MYTabBarViewController())
                })
        }
    }
    
    //准备要显示的ui
    private func prepareUI(){
        view.addSubview(bkgView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        //使用snpkit来加上约束
        //背景
        bkgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
        }
        //头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
            //要设置头像的大小，这样才不会少角
            make.size.equalTo(CGSize(width: 85, height: 85))
            
        }
        //消息框
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(self.iconView.snp_bottom).offset(16)
        }
        
    }
    
    /**
    *  懒加载
    */
    //背景UIImageView
    private lazy var bkgView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //头像UIImageView
    private lazy var iconView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        //设置圆角
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    //消息label
    private lazy var messageLabel:UILabel = {
        let label = UILabel()

        label.text = "欢迎归来"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(16)
        label.sizeToFit()
        label.alpha = 0
        return label
    }()
}








